// packageC/micro_communities/microClassification/microClassification.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    selected: 0,
    commodityTitle: '',
    classificationHide: true,
    catelist: [],
    catelistData: [],
    cateid: '',
    lastindex: 0,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    loading: false,
	
    goodsData: [],
    showList: true,
    micro_style_type:0,
    handleTag: false,
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.searchdata) {
      this.setData({
        commodityTitle:options.searchdata
      })
    }
    if (options.cateId) {
      this.data.cateid = options.cateId;
    }
    console.log(options)
    if(options.micro_style_type){
      this.setData({
        micro_style_type:options.micro_style_type
      })
    }
    this.classificationLabel();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (!this.data.isLoadMore) {
      console.log('没有更多数据');
    } else {
      this.getMoreData();
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  onShareTimeline:function(){},
  classificationHideBtn() {
    this.setData({
      classificationHide: true
    });
  },
  classificationLabel() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.index.getCate");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          res.data.data.unshift({
            child: [],
            icon: "",
            id: "",
            level: 1,
            name: "全部"
          })
          that.setData({
            catelist: res.data.data
          });
          if (that.data.catelist && that.data.catelist.length > 0) {
            let catelistData = that.data.catelistData;
            for (let i = 0; i < that.data.catelist.length; i++) {
              catelistData.push([]);
            }
            that.setData({
              catelistData: catelistData
            });
            if (that.data.cateid) {
              for (let i = 0; i < that.data.catelist.length; i++) {
                if (that.data.catelist[i].id == that.data.cateid) {
                  that.getClassification(true, i);
                  that.setData({
                    selected: i,
                    lastindex: i
                  });
                  break;
                }
              }
            } else {
              that.setData({
                cateid: that.data.catelist[0].id
              });
              that.getClassification(false);
            }
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getClassification(setindex, index) {
    let that = this;
    let dataJson = {};
    that.pageInit();
    this.setData({
      showList: false
    });
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.index.getStick");
    if (this.data.commodityTitle) {
      dataJson.title = this.data.commodityTitle;
    }
    if (this.data.cateid) {
      dataJson.cat_id = this.data.cateid;
    }
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: dataJson,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.total_page = res.data.last_page;
          if(res.data.micro_style_type){
            that.setData({
              micro_style_type:res.data.micro_style_type
            })
          }
          if (res.data.current_page >= res.data.last_page || res.data.data.length < res.data.per_page) {
            that.isLoadMore = false;
          }
          // 修改部分
          that.setData({
            goodsData: res.data.data,
            showList: true
          });
          console.log(that.data.goodsData)
          return;
          // 修改部分
          // let catelistData = that.data.catelistData;
          // if (setindex) {
          //   catelistData.splice(index, 1, res.data.data);
          // } else {
          //   catelistData.splice(0, 1, res.data.data);
          //   that.setData({
          //     lastindex: 0
          //   });
          // }
          // that.setData({
          //   catelistData: catelistData
          // });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {}
    });
  },
  commodityTitleClear() {
    this.setData({
      commodityTitle: ''
    });
  },
  commodityTitleInp(e) {
    let val = e.detail.value;
    this.setData({
      commodityTitle: val
    });
  },
  searchBtn() {
    this.setData({
      handleTag: true,
    })
    this.getClassification(true, this.data.lastindex);
  },
  swishTabTItem(e) {
    let index = e.detail.index;
    let id = this.data.catelist[index].id;
    this.setData({
      selected: index,
      lastindex: index,
      cateid: id,
      handleTag: true,
    });
    this.getClassification(true, index);
    this.setData({
      classificationHide: true
    });
  },
  swishTabTItemChild(e){
    let {index, id} = e.currentTarget.dataset;
    this.setData({
      selected: index,
      lastindex: index,
      cateid: id,
      handleTag: true,
    });
    this.getClassification(true, index);
    this.setData({
      classificationHide: true
    });
  },

  classificationShow() {
    this.setData({
      classificationHide: !this.data.classificationHide
    });
  },
  jumpDetails(e) {
    let stick_id = e.currentTarget.dataset.stickid;
    wx.navigateTo({
      url: '/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=' + stick_id
    });
  },
  pageInit() {
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;
    this.data.loading = false;
  },
  getMoreData() {
    const that = this;
    let json = {};
    if (this.data.commodityTitle) {
      json.title = this.data.commodityTitle;
    }
    if (this.data.cateid) {
      json.cat_id = this.data.cateid;
    }
    if (this.data.page >= this.data.total_page || this.data.loading) {
      return false;
    }
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.index.getStick");
    that.setData({
      loading: true
    });
    that.data.page = this.data.page + 1;
    json.page = this.data.page;
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: json,
      success: function(resdata) {
        var res = resdata.data;
        that.setData({
          loading: false,
        })
        if (res.result == 1) {
          var myData = res.data.data;
          that.setData({
            goodsData: that.data.goodsData.concat(myData),
            handleTag: false,
          });
          if (res.data.current_page >= res.data.last_page || myData.length < res.data.per_page) {
            that.isLoadMore = false;
          }
        } else {
          that.data.page = that.data.page - 1;
          that.data.isLoadMore = false;
          return;
        }
      },
      fail: function(res) {}
    });
  },
});
